//
//  EvaluationHeaderView.m
//  gdemm
//
//  Created by wu yong on 16/10/17.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "EvaluationHeaderView.h"

#import "Masonry.h"
#import "Utils.h"
#import "progrossView.h"
@interface EvaluationHeaderView ()

@property(nonatomic,strong)UIImageView *lineImage;
@property(nonatomic,strong)UIButton * iconBtn;
@property(nonatomic,strong)UIImageView *circleImageView;
@property (strong,nonatomic) NSTimer * animateTimer;
@property(strong,nonatomic)progrossView * disPlayView ;
@property(nonatomic,strong)UILabel *resultLabel;
@property(strong,nonatomic) NSTimer *timer;
@property(assign,nonatomic)NSInteger count;
@end

@implementation EvaluationHeaderView

-(instancetype)init{
    if (self == [super init]) {
         [self setUpHeaderView];
        //    添加动画定时器
        self.animateTimer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(startAnimate) userInfo:nil repeats:YES];
    }
    return self;
}

-(void)setEvaluationState:(EvaluationResult)evaluationState{
    _evaluationState = evaluationState;
    switch (evaluationState) {
        case Evaluating:{
            self.iconBtn.userInteractionEnabled = NO;
            self.resultLabel.text = @"正在测评...";
            self.disPlayView.hidden = NO;
             self.resultLabel.textColor = [Utils colorWithKey:@"circleColor"];
            [self startProgressHandler];
        }
            break;
        case EvaluationResultQualified:
            self.iconBtn.userInteractionEnabled = YES;
            self.resultLabel.text = @"测评合格";
            self.resultLabel.textColor = [Utils UIColorFromHex:0X80c269];
            [self.iconBtn setImage:[Utils getImageByDevce:@"ceping_icon_17"] forState:UIControlStateNormal];
            self.circleImageView.image = [Utils getImageByDevce:@"ceping_icon_18"];
            self.disPlayView.hidden = YES;
            break;
        case EvaluationResultUnualified:
            self.iconBtn.userInteractionEnabled = YES;
            self.resultLabel.text = @"测评不合格，请手动修复";
            self.resultLabel.textColor = [Utils UIColorFromHex:0Xf8b551];
            [self.iconBtn setImage:[Utils getImageByDevce:@"ceping_icon_16"] forState:UIControlStateNormal];
            self.circleImageView.image = [Utils getImageByDevce:@"ceping_icon_25"];
            self.disPlayView.hidden = YES;
            break;
        case EvaluationResultHigtRisk:
            self.iconBtn.enabled = YES;
            self.resultLabel.text = @"存在高危风险，请手动修复";
            self.resultLabel.textColor = [Utils UIColorFromHex:0Xf16b6f];
            [self.iconBtn setImage:[Utils getImageByDevce:@"ceping_icon_01"] forState:UIControlStateNormal];
            self.circleImageView.image = [Utils getImageByDevce:@"ceping_icon_24"];
            self.disPlayView.hidden = YES;
            break;
        default:
            break;
    }
}

//创建头视图
-(void)setUpHeaderView{
    
    self.lineImage =  [[UIImageView alloc]init];
    self.lineImage.image = [Utils getImageByDevce:@"pb_line"];
    self.lineImage.hidden = YES;
    [self addSubview:self.lineImage];
    
    self.circleImageView =  [[UIImageView alloc]init];
    self.circleImageView.image = [Utils getImageByDevce:@"ceping_icon_18"];
    [self addSubview:self.circleImageView];
    
    self.iconBtn = [[UIButton alloc]init];
    [self.iconBtn addTarget:self action:@selector(repairItems) forControlEvents:UIControlEventTouchUpInside];
    [self.iconBtn setImage:[Utils getImageByDevce:@"ceping_icon_17"] forState:UIControlStateNormal];
    [self.iconBtn sizeToFit];
    [self addSubview:self.iconBtn];
    
    self. disPlayView = [[progrossView alloc]init];
    self.disPlayView.hidden = YES;
    [self addSubview:self.disPlayView];
    
    self.resultLabel = [[UILabel alloc]init];
    self.resultLabel.text = @"测评合格";
    [self.resultLabel setFont:[UIFont systemFontOfSize:22]];
    [self.resultLabel setTextColor:[Utils UIColorFromHex:0X80c269]];
    [self addSubview:self.resultLabel];
}

-(void)startAnimate{
    if (self.evaluationState == Evaluating) {
        return;
    }
    self.lineImage.hidden = YES;
    [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.circleImageView.transform = CGAffineTransformMakeScale(1.1, 1.1);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.circleImageView.transform = CGAffineTransformMakeScale(1.0, 1.0);
        } completion:^(BOOL finished) {
            // 添加组动画
            self.lineImage.hidden = NO;
            CABasicAnimation * baseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
            baseAnimation.fromValue = [NSNumber numberWithFloat:1.0];
            baseAnimation.toValue = [NSNumber numberWithFloat:1.2];
            
            CABasicAnimation * opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
            opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];
            opacityAnimation.toValue = [NSNumber numberWithFloat:0.2];
            
            CAAnimationGroup * groupAnimate = [CAAnimationGroup animation];
            groupAnimate.animations = @[baseAnimation,opacityAnimation];
            groupAnimate.duration = 1;
            groupAnimate.repeatCount = 2;
            groupAnimate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            [self.lineImage.layer addAnimation:groupAnimate forKey:nil];
        }];
    }];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    if ([DeviceTypeJudge isPadDeviceType]) {
        [self.disPlayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            make.top.mas_equalTo(self.mas_top).offset(53);
            make.height.width.mas_equalTo(270);
        }];
        
        [self.lineImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            make.top.mas_equalTo(self.mas_top).offset(70);
            make.height.width.mas_equalTo(232);
        }];
        
        [self.circleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self.lineImage);
            make.height.width.mas_equalTo(217);
        }];
        [self.iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self.lineImage);
        }];
        [self.resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.circleImageView.mas_bottom).offset(24);
            make.centerX.mas_equalTo(self);
        }];
         [self.resultLabel setFont:[UIFont systemFontOfSize:22]];
    }else{
        [self.disPlayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            //make.top.mas_equalTo(self.mas_top).offset(10);
            make.height.width.mas_equalTo(150*KHeightScale);
        }];
        
        [self.lineImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            make.top.mas_equalTo(self.mas_top).offset(10);
            make.height.width.mas_equalTo(130*KHeightScale);
        }];
        
        [self.circleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self.lineImage);
            make.height.width.mas_equalTo(125*KHeightScale);
        }];
        [self.iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self.lineImage);
//            make.width.height.mas_equalTo(44);
        }];
        [self.resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.lineImage.mas_bottom).offset(20);
            make.centerX.mas_equalTo(self);
        }];
      [self.resultLabel setFont:[UIFont systemFontOfSize:15]];
        
    }
}

- (void)onClickBtnBlock:(onClickBlock)clickBlock{
    self.clickBlock = clickBlock;
}

- (void)repairItems{
    self.clickBlock();
}
-(void)dealloc{
    [self.animateTimer invalidate];
    self.animateTimer = nil;
}

#pragma mark -开始测评
-(void)startTest
{
    self.evaluationState = Evaluating;
}
- (void)startProgressHandler{
    _count = 0;
    [self.timer invalidate];
    self.timer =  [NSTimer  scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(handler) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)handler{
    
    if (_count < 100) {
        _count += 1;
        self.disPlayView.progress = _count;
    }else{
        [[NSNotificationCenter  defaultCenter]postNotificationName:progrossViewNotification object:nil];
        _count = 0;
        [self.timer invalidate];
        
    }
}
@end
